Add "extended" file metadata to API
[lhc/web/wiklou.git] / includes / filerepo / file / ForeignAPIFile.php
old mode 100644 (file)
new mode 100755 (executable)
index edf623e..a1a6f26
@@ -54,22 +54,22 @@ class ForeignAPIFile extends File {
         */
        static function newFromTitle( Title $title, $repo ) {
                $data = $repo->fetchImageQuery( array(
-                       'titles'            => 'File:' . $title->getDBkey(),
-                       'iiprop'            => self::getProps(),
-                       'prop'              => 'imageinfo',
+                       'titles' => 'File:' . $title->getDBkey(),
+                       'iiprop' => self::getProps(),
+                       'prop' => 'imageinfo',
                        'iimetadataversion' => MediaHandler::getMetadataVersion()
                ) );
 
                $info = $repo->getImageInfo( $data );
 
-               if( $info ) {
+               if ( $info ) {
                        $lastRedirect = isset( $data['query']['redirects'] )
                                ? count( $data['query']['redirects'] ) - 1
                                : -1;
-                       if( $lastRedirect >= 0 ) {
+                       if ( $lastRedirect >= 0 ) {
                                $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
                                $img = new self( $newtitle, $repo, $info, true );
-                               if( $img ) {
+                               if ( $img ) {
                                        $img->redirectedFrom( $title->getDBkey() );
                                }
                        } else {
@@ -86,7 +86,7 @@ class ForeignAPIFile extends File {
         * @return string
         */
        static function getProps() {
-               return 'timestamp|user|comment|url|size|sha1|metadata|mime';
+               return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype';
        }
 
        // Dummy functions...
@@ -111,7 +111,7 @@ class ForeignAPIFile extends File {
         * @return bool|MediaTransformOutput
         */
        function transform( $params, $flags = 0 ) {
-               if( !$this->canRender() ) {
+               if ( !$this->canRender() ) {
                        // show icon
                        return parent::transform( $params, $flags );
                }
@@ -119,12 +119,25 @@ class ForeignAPIFile extends File {
                // Note, the this->canRender() check above implies
                // that we have a handler, and it can do makeParamString.
                $otherParams = $this->handler->makeParamString( $params );
+               $width = isset( $params['width'] ) ? $params['width'] : -1;
+               $height = isset( $params['height'] ) ? $params['height'] : -1;
 
                $thumbUrl = $this->repo->getThumbUrlFromCache(
                        $this->getName(),
-                       isset( $params['width'] ) ? $params['width'] : -1,
-                       isset( $params['height'] ) ? $params['height'] : -1,
-                       $otherParams );
+                       $width,
+                       $height,
+                       $otherParams
+               );
+               if ( $thumbUrl === false ) {
+                       global $wgLang;
+                       return $this->repo->getThumbError(
+                               $this->getName(),
+                               $width,
+                               $height,
+                               $otherParams,
+                               $wgLang->getCode()
+                       );
+               }
                return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
        }
 
@@ -156,16 +169,26 @@ class ForeignAPIFile extends File {
                return null;
        }
 
+       /**
+        * @return array|null extended metadata (see imageinfo API for format) or null on error
+        */
+       public function getExtendedMetadata() {
+               if ( isset( $this->mInfo['extmetadata'] ) ) {
+                       return $this->mInfo['extmetadata'];
+               }
+               return null;
+       }
+
        /**
         * @param $metadata array
         * @return array
         */
        public static function parseMetadata( $metadata ) {
-               if( !is_array( $metadata ) ) {
+               if ( !is_array( $metadata ) ) {
                        return $metadata;
                }
                $ret = array();
-               foreach( $metadata as $meta ) {
+               foreach ( $metadata as $meta ) {
                        $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
                }
                return $ret;
@@ -224,7 +247,7 @@ class ForeignAPIFile extends File {
         * @return string
         */
        function getMimeType() {
-               if( !isset( $this->mInfo['mime'] ) ) {
+               if ( !isset( $this->mInfo['mime'] ) ) {
                        $magic = MimeMagic::singleton();
                        $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
                }
@@ -232,10 +255,12 @@ class ForeignAPIFile extends File {
        }
 
        /**
-        * @todo FIXME: May guess wrong on file types that can be eg audio or video
         * @return int|string
         */
        function getMediaType() {
+               if ( isset( $this->mInfo['mediatype'] ) ) {
+                       return $this->mInfo['mediatype'];
+               }
                $magic = MimeMagic::singleton();
                return $magic->getMediaType( null, $this->getMimeType() );
        }